$(function(){
    window.startDate = $('#doc').data('today');
    window.roomItemType = {};
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var type = getParams.type;
    if(currentHotel&&type){
        $('#doc-header-dropdown').find('a[data-hotel|='+currentHotel+']').addClass('active');
        $('#doc-info-dropdown').find('a[data-value|='+type+']').addClass('active');
        $('#filterSearch').attr('href','/pushNews.html?hotel='+currentHotel+'&type='+type+'&keys=');
    }else{
        if(currentHotel){
            $('#doc-header-dropdown').find('a[data-hotel|='+currentHotel+']').addClass('active');
            $('#filterSearch').attr('href','/pushNews.html?hotel='+currentHotel+'&keys=');
        }else{
            $('#doc-header-dropdown').find('a[data-hotel|=0]').addClass('active');
        }
        if(type){
            $('#doc-info-dropdown').find('a[data-value|='+type+']').addClass('active');
            $('#filterSearch').attr('href','/pushNews.html?type='+type+'&keys=');
        }else{
            $('#doc-info-dropdown').find('a[data-value|=3]').addClass('active');
        }          
    }
    $('#filterRoomMenu li').each(function (index, item) {
        var liItem = $(item);
        var value = liItem.data('value');
        var name = liItem.data('name');
        var typeValue = liItem.data('type-value');
        var price = liItem.data('price');
        window.roomItemType[value] = {
            'value': value,
            'name': name,
            'type': typeValue,
            'price': price,
            'from': liItem.data('from')
        };
    });
    var push_content = $('#push_content');
    push_content.on('click','.btn-close',function(e){
        //删除推送消息
        e.stopPropagation();  
        var submitBtn = $(this);
        var newsPushRelateId = submitBtn.data('relateid');
        var newsPushId = submitBtn.data('pushid');
        var currentHotel = submitBtn.closest('.message_list').data('hotel');
        ajaxRequest('/Home/NewsPush/saveNews', {
            "hotel": currentHotel,
            "newsPushRelateId":newsPushRelateId,
            "newsPushId":newsPushId,
            "type": 2,
        }, 'post', function(data) {
            submitBtn.closest('.message_list').remove();
        }, function(error) { 
        }); 
    }).on('click','.operator',function(e){
        //操作人列表
        e.stopPropagation();  
        var submitBtn = $(this);
        var newsPushId = submitBtn.data('pushid');
        var currentHotel = submitBtn.closest('.message_list').data('hotel');
        $('#operator_list').modal('show');
        var templatePushMessage = $($('#templatePushMessage').clone().html());
        var pushMessageHTML = '';
        ajaxRequest('/Home/NewsPush/newsHistory', {
            "hotel": currentHotel,
            "newsPushId":newsPushId,
        }, 'post', function(data) {
            $('#operatorListBody').html('');
            $('#no_operatorListBody').addClass('hidden');
            $('#operatorListBody').removeClass('hidden');
            var info = data.data;
            for(var i=0;i<info.newsList.length;i++){
               templatePushMessage.find('.info_time').text(info.newsList[i].startTime);
               templatePushMessage.find('.info_name').text(info.newsList[i].operator);
               templatePushMessage.find('.info_duan').text(info.newsList[i].channel);
               templatePushMessage.find('.info_operation').text(info.newsList[i].operateType);
               pushMessageHTML += templatePushMessage.prop('outerHTML');
            }      
            $('#operatorListBody').append(pushMessageHTML);
        }, function(error) { 
            hideMainLoading();
            if(error.responseJSON.status == 801){
                $('#operatorListBody').addClass('hidden');
                $('#no_operatorListBody').removeClass('hidden');
            }
        });        
    });
    //查询
    $('#filterName').on('blur',function(){
        var val = $(this).val();
        var origHref = $('#filterSearch').attr('href');
        $('#filterSearch').attr('href',origHref+val);
    });
    $('#doc-header-dropdown').on('click','li>a',function(){
        var submitBtn = $(this);
        var hotel =  submitBtn.data('hotel'); 
        $('#doc-header-brand .nowrap-content').text(submitBtn.text());
        submitBtn.addClass('active').closest('li').siblings('li').find('a').removeClass('active');
        submitBtn.closest('#navbar-hotel-switch').attr('data-currenthotel',hotel);
        if(type){
            if(hotel == 0){
                window.location.href = '/pushNews.html?type='+type;
            }else{
                window.location.href = '/pushNews.html?hotel='+hotel+'&type='+type;
            }
        }else{
            if(hotel == 0){
                window.location.href = '/pushNews.html';
            }else{
                window.location.href = '/pushNews.html?hotel='+hotel;
            }
        }
    });
    $('#doc-info-dropdown').on('click','li>a',function(){
        var submitBtn = $(this); 
        var value =  submitBtn.data('value');
        $('#doc-header-info .nowrap-content').text(submitBtn.text());
        submitBtn.addClass('active').closest('li').siblings('li').find('a').removeClass('active');
        submitBtn.closest('#navbar-info-switch').attr('data-value',value);
        if(currentHotel){
            if(value == 3){
                window.location.href = '/pushNews.html?hotel='+currentHotel;
            }else{
                window.location.href = '/pushNews.html?hotel='+currentHotel+'&type='+value;
            }             
        }else{
            if(value == 3){
                window.location.href = '/pushNews.html';
            }else{
                window.location.href = '/pushNews.html?type='+value;
            }             
        }
       
    });    
});

var getArgs = function getArgs() {
    var args = {};
    var query = location.search.substring(1);
    // Get query string
    var pairs = query.split("&");
    // Break at ampersand
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        // Look for "name=value"
        if (pos == -1) {
            continue;
        }
        // If not found, skip
        var argname = pairs[i].substring(0, pos); // Extract the name
        var value = pairs[i].substring(pos + 1); // Extract the value
        value = decodeURIComponent(value); // Decode it, if needed
        args[argname] = value;
        // Store as a property
    }
    return args; // Return the object
};